<?php 
/**
 * 获取IP地址
 *
 * @return string
 */
// function getip(){
//     if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
//         $ip = getenv("HTTP_CLIENT_IP");
//         else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
//             $ip = getenv("HTTP_X_FORWARDED_FOR");
//             else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
//                 $ip = getenv("REMOTE_ADDR");
//                 else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
//                     $ip = $_SERVER['REMOTE_ADDR'];
//                     else
//                         $ip = "unknown";
//                         return($ip);
// }

// function getip(){
//     $ip=false;
//     if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//         $ip=$_SERVER['HTTP_CLIENT_IP'];
//     }
//     if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//         $ips=explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
//         if($ip){
//             array_unshift($ips, $ip);
//             $ip=FALSE;
//         }
//         for ($i=0; $i < count($ips); $i++){
//             if(!eregi ('^(10│172.16│192.168).', $ips[$i])){
//                 $ip=$ips[$i];
//                 break;
//             }
//         }
//     }
//     return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
// }

function getip() {
    static $realip;
    if(isset($_SERVER)){
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $realip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else if(isset($_SERVER['HTTP_CLIENT_IP'])){
            $realip=$_SERVER['HTTP_CLIENT_IP'];
        }
        else{
            $realip=$_SERVER['REMOTE_ADDR'];
        }
    }
    else{
        if(getenv('HTTP_X_FORWARDED_FOR')){
            $realip=getenv('HTTP_X_FORWARDED_FOR');
        }
        else if(getenv('HTTP_CLIENT_IP')){
            $realip=getenv('HTTP_CLIENT_IP');
        }
        else{
            $realip=getenv('REMOTE_ADDR');
        }
    }
    return $realip;
}

/**
 获取 IP  地理位置
 * 淘宝IP接口
 * @Return: array
 */
function getCity($ip = '')
{
    $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
    $ip=json_decode(file_get_contents($url));
    if((string)$ip->code=='1'){
        return false;
    }
    $data = (array)$ip->data;
    return $data;
}


function get_ip(){
    //判断服务器是否允许$_SERVER
    if(isset($_SERVER)){
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }elseif(isset($_SERVER['HTTP_CLIENT_IP'])) {
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        }else{
            $realip = $_SERVER['REMOTE_ADDR'];
        }
    }else{
        //不允许就使用getenv获取
        if(getenv("HTTP_X_FORWARDED_FOR")){
            $realip = getenv( "HTTP_X_FORWARDED_FOR");
        }elseif(getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        }else{
            $realip = getenv("REMOTE_ADDR");
        }
    }
    
    return $realip;
}  

function getRip(){
    $ip = get_ip();
    if($ip=='127.0.0.1'){
        $myIp = 'myip';
    }
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, 'http://ip.taobao.com/service/getIpInfo.php?ip='.$myIp);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //执行命令
    curl_multi_getcontent( $curl );
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    
    //显示获得的数据
    return json_decode($data,true);
}

function weather($chengshi){
    $url = 'http://wthrcdn.etouch.cn/weather_mini?city='.urlencode($chengshi);
    $html = file_get_contents($url);
    $jsondata = gzdecode($html);
    $data=json_decode($jsondata,true);
    
    $arr=array();
    $arr['chengshi']=$data['data']['city'];
    $dangtian=$data['data']['forecast'][0];
    $arr['gaowen']= str_replace("高温 ",null,$dangtian['high']);
    $arr['diwen']= str_replace("低温 ",null,$dangtian['low']);
    $arr['tianqi']=$dangtian['type'];
    return $arr;
}


/**
 * 基础分页的相同代码封装，使前台的代码更少
 * @param $count 要分页的总记录数
 * @param int $pagesize 每页查询条数
 * @return \Think\Page
 */
function getpage($count, $pagesize = 10) {
    $p = new \Think\HomePage($count, $pagesize);
    $p->setConfig('header', '');
    $p->setConfig('prev', '<');
    $p->setConfig('next', '>');
    $p->setConfig('last', '末页');
    $p->setConfig('first', '首页');
    $p->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
    $p->lastSuffix = false;//最后一页不显示为总页数
    return $p;
}


function deep_in_array($value, $array) {
    foreach($array as $item) {
        if(!is_array($item)) {
            if ($item == $value) {
                return true;
            } else {
                continue;
            }
        }
        if(in_array($value, $item)) {
            return true;
        } else if(deep_in_array($value, $item)) {
            return true;
        }
    }
    return false;
}

?>